package com.sjd.web.controller.farm;

import com.sjd.common.annotation.Log;
import com.sjd.common.core.controller.BaseController;
import com.sjd.common.core.domain.AjaxResult;
import com.sjd.common.core.page.TableDataInfo;
import com.sjd.common.enums.BusinessType;
import com.sjd.common.utils.poi.ExcelUtil;
import com.sjd.farm.domain.SjdFarmLand;
import com.sjd.farm.domain.SjdFarmLandChange;
import com.sjd.farm.domain.SjdFarmLandClaimOrder;
import com.sjd.farm.service.ISjdFarmLandChangeService;
import com.sjd.farm.service.ISjdFarmLandClaimOrderService;
import com.sjd.farm.service.ISjdFarmLandService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 农场土地信息Controller
 *
 * @author joker
 * @date 2022-01-24
 */
@RestController
@RequestMapping("/farm/land")
@AllArgsConstructor
@Slf4j
public class SjdFarmLandController extends BaseController {
    private final ISjdFarmLandService sjdFarmLandService;
    private final ISjdFarmLandChangeService sjdFarmLandChangeService;
    private final ISjdFarmLandClaimOrderService sjdFarmLandClaimOrderService;

    /**
     * 查询农场土地信息列表
     */
    @PreAuthorize("@ss.hasPermi('farm:land:list')")
    @GetMapping("/list")
    public TableDataInfo list(SjdFarmLand sjdFarmLand) {
        startPage();
        List<SjdFarmLand> list = sjdFarmLandService.selectSjdFarmLandList(sjdFarmLand);
        return getDataTable(list);
    }

    /**
     * 导出农场土地信息列表
     */
    @PreAuthorize("@ss.hasPermi('farm:land:export')")
    @Log(title = "农场土地信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SjdFarmLand sjdFarmLand) {
        List<SjdFarmLand> list = sjdFarmLandService.selectSjdFarmLandList(sjdFarmLand);
        ExcelUtil<SjdFarmLand> util = new ExcelUtil<>(SjdFarmLand.class);
        util.exportExcel(response, list, "农场土地信息数据");
    }

    /**
     * 获取农场土地信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('farm:land:query')")
    @GetMapping(value = "/{landId}")
    public AjaxResult getInfo(@PathVariable("landId") Long landId) {
        return AjaxResult.success(sjdFarmLandService.selectSjdFarmLandByLandId(landId));
    }

    /**
     * 新增农场土地信息
     */
    @PreAuthorize("@ss.hasPermi('farm:land:add')")
    @Log(title = "农场土地信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SjdFarmLand sjdFarmLand) {
        return toAjax(sjdFarmLandService.insertSjdFarmLand(sjdFarmLand));
    }

    /**
     * 修改农场土地信息
     */
    @PreAuthorize("@ss.hasPermi('farm:land:edit')")
    @Log(title = "农场土地信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SjdFarmLand sjdFarmLand) {
        return toAjax(sjdFarmLandService.updateSjdFarmLand(sjdFarmLand));
    }

    /**
     * 上下架农场信息
     */
    @PreAuthorize("@ss.hasPermi('farm:land:status')")
    @Log(title = "农场土地信息上下架", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/status/{landFlag}/{oldStatus}/{newStatus}")
    public AjaxResult updateStatus(@PathVariable String landFlag,
                                   @PathVariable int oldStatus,
                                   @PathVariable int newStatus) {
        if (SjdFarmLand.Param.CLAIM == oldStatus) {
            return AjaxResult.error("操作失败：已认领的土地不允许上下架！！！");
        }
        int rows = sjdFarmLandService.batchHandleStatus(landFlag, oldStatus, newStatus);
        if (rows == 0) {
            return AjaxResult.error("状态更新失败：没有可操作的土地数据！！！");
        }
        return AjaxResult.success();
    }


    /**
     * 查询农场土地信息变更列表
     */
    @PreAuthorize("@ss.hasPermi('farm:land:change:list')")
    @GetMapping("/change/list")
    public TableDataInfo list(SjdFarmLandChange sjdFarmLandChange) {
        startPage();
        List<SjdFarmLandChange> list = sjdFarmLandChangeService.selectSjdFarmLandChangeList(sjdFarmLandChange);
        return getDataTable(list);
    }

    /**
     * 导出农场土地信息变更列表
     */
    @PreAuthorize("@ss.hasPermi('farm:land:change:export')")
    @Log(title = "农场土地信息变更", businessType = BusinessType.EXPORT)
    @PostMapping("/change/export")
    public void export(HttpServletResponse response, SjdFarmLandChange sjdFarmLandChange) {
        List<SjdFarmLandChange> list = sjdFarmLandChangeService.selectSjdFarmLandChangeList(sjdFarmLandChange);
        ExcelUtil<SjdFarmLandChange> util = new ExcelUtil<>(SjdFarmLandChange.class);
        util.exportExcel(response, list, "农场土地信息变更数据");
    }

    /**
     * 查询农场土地信息变更列表
     */
    @PreAuthorize("@ss.hasPermi('farm:land:claim:order:list')")
    @GetMapping("/claim/order/list")
    public TableDataInfo list(SjdFarmLandClaimOrder sjdFarmLandClaimOrder) {
        startPage();
        List<SjdFarmLandClaimOrder> list = sjdFarmLandClaimOrderService.selectSjdFarmLandClaimOrderList(sjdFarmLandClaimOrder);
        return getDataTable(list);
    }

    /**
     * 导出农场土地信息变更列表
     */
    @PreAuthorize("@ss.hasPermi('farm:land:claim:order:export')")
    @Log(title = "农场土地认领订单", businessType = BusinessType.EXPORT)
    @PostMapping("/claim/order/export")
    public void export(HttpServletResponse response, SjdFarmLandClaimOrder sjdFarmLandClaimOrder) {
        List<SjdFarmLandClaimOrder> list = sjdFarmLandClaimOrderService.selectSjdFarmLandClaimOrderList(sjdFarmLandClaimOrder);
        ExcelUtil<SjdFarmLandClaimOrder> util = new ExcelUtil<>(SjdFarmLandClaimOrder.class);
        util.exportExcel(response, list, "农场土地认领订单数据");
    }
}
